const std = @import("std");

const ver_new = @hasDecl(std, "Build");

const build_type = if(ver_new) std.Build else std.build.Builder;

pub fn build(b: *build_type) !void {
    const target = b.standardTargetOptions(.{});
    const mode = if(ver_new)
        b.standardOptimizeOption(.{})
    else
        b.standardReleaseOptions();

    var buf: [50]u8 = undefined;
    var buf1: [50]u8 = undefined;

    for ([_][]const u8{"client"}) |name| {
        const path = try std.fmt.bufPrint(&buf, "src/{s}.zig", .{name});
        const exe = if(ver_new)
            b.addExecutable(.{
                .name = name,
                .root_source_file = .{ .path = path },
                .target = target,
                .optimize = mode,
            })
        else
            b.addExecutable(name, path);
        if(ver_new) {
            b.installArtifact(exe);
        } else {
            exe.setTarget(target);
            exe.setBuildMode(mode);
            exe.install();
        }
        const run_cmd = if(ver_new)
            b.addRunArtifact(exe)
        else
            exe.run();
        run_cmd.step.dependOn(b.getInstallStep());
        if (b.args) |args| {
            run_cmd.addArgs(args);
        }
        const run_name = try std.fmt.bufPrint(&buf, "run_{s}", .{name});
        const run_detail = try std.fmt.bufPrint(&buf1, "Run the {s}", .{name});
        const run_step = b.step(run_name, run_detail);
        run_step.dependOn(&run_cmd.step);
    }
}
